package com.woniuxy.controller;

import com.alibaba.excel.EasyExcel;
import com.woniuxy.entity.Merchants;
import com.woniuxy.entity.Organization;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.OrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/organization")
@Api(tags = "组织接口")
@RequiresRoles(value = {"superAdmin","admin"},logical = Logical.OR)
public class OrganizationController {
    @Autowired(required = false)
    OrganizationService organizationService;

    @GetMapping("/{currentPage}/{pageSize}")
    @ApiOperation(value = "获取分页组织数据")
    public ResponseEntity findAll(@ApiParam(name = "currentPage", value="当前页码",required=true)@PathVariable int currentPage,
                                  @ApiParam(name = "pageSize", value="页面大小",required=true)@PathVariable int pageSize){
        return organizationService.findAll(currentPage, pageSize);
    }

    @GetMapping("/findParent")
    @ApiOperation(value = "获取所有顶级父类组织数据")
    public ResponseEntity findParent(){
        return organizationService.findParent();
    }

    @GetMapping("/type")
    @ApiOperation(value = "获取组织类别")
    public ResponseEntity findType(){
        return organizationService.findType();
    }

    @GetMapping("/attr")
    @ApiOperation(value = "获取组织属性")
    public ResponseEntity findAttr(){
        return organizationService.findAttr();
    }

    @GetMapping("/category")
    @ApiOperation(value = "获取组织分类")
    public ResponseEntity findCategory(){
        return organizationService.findCategory();
    }

    @RequiresPermissions(value = "organization:add")
    @PostMapping("/add")
    @ApiOperation(value = "新增组织")
    public ResponseEntity addOne(@ApiParam(name = "organization", value="新增组织实体类",required=true)@RequestBody Organization organization){
        return organizationService.addOne(organization);
    }

    @RequiresPermissions(value = "organization:update")
    @PutMapping("/update")
    @ApiOperation(value = "修改组织")
    public ResponseEntity updateOne(@ApiParam(name = "organization", value="修改组织实体类",required=true)@RequestBody Organization organization){
        return organizationService.updateOne(organization);
    }

    @RequiresPermissions(value = "organization:delete")
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除组织")
    public ResponseEntity deleteOne(@ApiParam(name = "id", value="组织序号",required=true)@PathVariable int id){
        return organizationService.deleteOne(id);
    }

    @PostMapping("/search")
    @ApiOperation(value = "查询组织")
    public ResponseEntity findByContext(@ApiParam(name = "organization", value="组织实体",required=true)@RequestBody Organization organization){
        return organizationService.findByContext(organization);
    }

    @GetMapping("/all")
    @ApiOperation(value = "获取所有组织")
    public ResponseEntity findOrganization(){
        return organizationService.findOrganization();
    }

    @RequiresPermissions(value = "organization:batchDelete")
    @PutMapping("/batchDelete")
    @ApiOperation(value = "批量删除组织")
    public ResponseEntity batchDelete(@ApiParam(name = "ids", value="组织id数组",required=true)@RequestBody int[] ids){
        return organizationService.batchDelete(ids);
    }

    @RequiresPermissions(value = "organization:import")
    @PostMapping("/import")
    @ApiOperation(value = "从excel导入")
    public ResponseEntity importExcel(@ApiParam(name = "file", value="excel文件")@RequestPart("file") MultipartFile file) {
        List<Organization> list = null;
        try {
            list = EasyExcel.read(file.getInputStream())
                    .head(Organization.class)
                    .sheet()
                    .doReadSync();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return organizationService.importExcel(list);
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出excel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        // 这里URLEncoder.encode可以防止中文乱码，所有通过后端的文件下载都可以如此处理
        String fileName = URLEncoder.encode("组织列表.xlsx", "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf8");
        //建议加上该段，否则可能会出现前端无法获取Content-disposition
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName );
        EasyExcel.write(response.getOutputStream(), Organization.class).sheet("模板")
                .doWrite((Collection<?>) organizationService.findAll().getData());
    }

    @GetMapping("/no/{no}")
    @ApiOperation(value = "根据编码查询组织编码")
    public ResponseEntity findByNo(@ApiParam(name = "no", value="组织编码") @PathVariable String no){
        return organizationService.findByNo(no);
    }
}
